// pages/m_address/m_address.js
const getToken = require('../../utils/get_token.js');
let memberId;
let cookie, token;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    demo(that);
  } else {
    let tokens = await getToken.getToken();
    demo(that);
  }
}

const demo = function (that) {
  let params = ['common/receiver/list', {"id":memberId}]
  getToken.getData(function (res) {
    that.setData({
      list: res.data.lists
    })
  }, ...params)
}
Page({

  /**
   * 页面的初始数据
   */
	data: {
		boolT: true,
    pages:'',
    gid:'',
		list: []
	},

  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
    let that = this;
    that.data.pages=options.page;
    that.data.gid = options.id;
		memberId = wx.getStorageSync('memberId');
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
	},
  onShow: function () {
    let that = this;
    ckToken(that);
  },
	changeDef: function (e) {//改变默认地址
    let aId = e.currentTarget.dataset.id;
    let that = this;
		if (that.data.boolT) {
			that.data.boolT = false;
      let params = ['common/receiver/settingDefault', { "memberId": memberId, "id": aId }]
      getToken.getData(function (res) {
        if (res.data.flag) {
          demo(that);

          wx.redirectTo({
            url: `../${that.data.pages}/${that.data.pages}?id=${that.data.gid}`,
            success: function () {
              setTimeout(function () { that.data.boolT = true; }, 500)
            }
          })
        }
      }, ...params)
		}
	},
  go_add:function(e){
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../m_address/m_address',
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
  }
})